package com.example.litingting.adapterdemo;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by litingting on 2016/3/21.
 */
public class MyAdapter extends BaseAdapter {
    private List<News> data;
    private LayoutInflater inflater;
    public MyAdapter(Context context,List<News> data){
        this.data=data;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vg = inflater.inflate(R.layout.news_layout,null);
        ImageView snap =(ImageView)vg.findViewById(R.id.snap);
        TextView title = (TextView)vg.findViewById(R.id.title);
        TextView content = (TextView)vg.findViewById(R.id.content);
        snap.setBackgroundResource(data.get(position).getSnap());
        title.setText(data.get(position).getTitle());
        content.setText(data.get(position).getContent());
        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("li",data.get(position).getContent());
            }
        });
        return vg;
    }
}
